/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import Logica.*;

/**
 *
 * @author Chino
 */
@ManagedBean
@RequestScoped
public class mbUsuario {
    private Encriptador encriptador = new Encriptador();
    private String nickname;
    private String contraseña;
    private final HttpServletRequest httpServletRequest;
    private final FacesContext faceContext;
    private FacesMessage facesMessage;
    
    public FacesMessage getFacesMessage() {
        return facesMessage;
    }

    public void setFacesMessage(FacesMessage facesMessage) {
        this.facesMessage = facesMessage;
    }
    
    
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getContraseña() {
        return contraseña;
    }

    public void setContraseña(String contraseña) {
        this.contraseña = contraseña;
    }
   
    public mbUsuario() {
        faceContext=FacesContext.getCurrentInstance();
        httpServletRequest=(HttpServletRequest)faceContext.getExternalContext().getRequest();
    }
    public String registrar(){
        double[][] encriptado= encriptador.Encriptar(contraseña);        
        int[] en=encriptador.linealizar(encriptado);
        org.tempuri.ArrayOfInt a = new org.tempuri.ArrayOfInt();
        for(int d=0;d<en.length;d++){
            a.getInt().add(en[d]);
        }
        boolean r= insertarListaOrdenada(a, contraseña);
        if(r){            
            facesMessage=new FacesMessage(FacesMessage.SEVERITY_INFO, "Registrado con exito", null);
            nickname="";
            faceContext.addMessage("form1:btnRegistrarme", facesMessage);            
        }else{
            facesMessage=new FacesMessage(FacesMessage.SEVERITY_ERROR, "El nickname ya esta en uso", null);
            nickname="";
            faceContext.addMessage("form1:btnRegistrarme", facesMessage);            
        }
        return "index";
    }
    
    public String login(){
        double[][] encriptado= encriptador.Encriptar(contraseña);        
        int[] en=encriptador.linealizar(encriptado);        
        org.tempuri.ArrayOfInt a = new org.tempuri.ArrayOfInt();
        for(int d=0;d<en.length;d++){
            a.getInt().add(en[d]);
        }
        boolean r = login(nickname,a);                        
        if(r){            
            httpServletRequest.getSession().setAttribute("Usuario", nickname);             
            return "Bienvenida";
        }else{
            facesMessage=new FacesMessage(FacesMessage.SEVERITY_ERROR, "Usuario o contraseña incorrectos", null);
            faceContext.addMessage("login_form:btn", facesMessage);
            return "index";
        }
    }


    private static boolean login(java.lang.String nickname, org.tempuri.ArrayOfInt encriptado) {
        org.tempuri.Estructuras service = new org.tempuri.Estructuras();
        org.tempuri.EstructurasSoap port = service.getEstructurasSoap();
        return port.login(nickname, encriptado);
    }

    private static boolean insertarListaOrdenada(org.tempuri.ArrayOfInt encriptado, java.lang.String nickname) {
        org.tempuri.Estructuras service = new org.tempuri.Estructuras();
        org.tempuri.EstructurasSoap port = service.getEstructurasSoap();
        return port.insertarListaOrdenada(encriptado, nickname);
    }
   

    
}
